function changeelementext(obj,id){
	var temp=jQ(obj);
	if(temp.hasClass("ext")){
		temp.removeClass("ext");
		jQ("#"+id).find("div.bottom").find("div.detailcontent").fadeIn("normal",function(){
			temp.addClass("extclose");
		});
	}else{
		temp.removeClass("extclose");
		jQ("#"+id).find("div.bottom").find("div.detailcontent").fadeOut("normal",function(){
			temp.addClass("ext");
		});
	}
}
function sendInvite(obj,id){
	var contactid=id.split("-")[1];
	jQ.ajax({
		type : "POST",
		url : "contactAjax!sendInvite.action",
		data : {
			id:contactid
		},
		success:function(data){
			dataObj=eval("("+data+")");
			clip_tips(dataObj['result']);
		}
	});
}
function editContact(obj){
	var _input = jQ(obj).parent().prevAll("li.editinput").find("input");
	_input.addClass("edit").attr("disabled","");
	var _parent=jQ(obj).parent();
	_parent.find("a.confirm").show();
	_parent.find("a.cancel").show();
	_parent.find("a.del").hide();
	_parent.find("a.edit").hide();
}
function confirmEdit(obj,id){
	var contactid=id.split("-")[1];
	var _input = jQ(obj).parent().prevAll("li.editinput").find("input");
	var name=jQ.trim(_input.val());
	if(name.length<=0||name.length>15){
		clip_tips("请输入1-15字符的姓名");
		return;
	}
	jQ.ajax({
		type:"POST",
		url: "contactAjax!saveContact.action",
		data:{
			id : contactid,
			contactName:name
		},
		success:function(data){
			dataObj=eval("("+data+")");
			if(dataObj['flag']){
				_input.attr("title",name);
				_input.val(name);
				_input.removeClass("edit").attr("disabled","disabled");
				var _parent=jQ(obj).parent();
				_parent.find("a.confirm").hide();
				_parent.find("a.cancel").hide();
				_parent.find("a.del").show();
				_parent.find("a.edit").show();
			}else{
				clip_tips(dataObj['result']);
			}
		}
	});
}
function delContact(obj,id){
	var contactid=id.split("-")[1];
	if(!confirm("确认删除"))
		return false;
	jQ.ajax({
		type : "POST",
		url : "contactAjax!delContact.action",
		data :{
			id : contactid
		},
		success:function(data){
			dataObj=eval("("+data+")");
			if(dataObj['flag']){
				jQ("#"+id).remove();
				getCountsAboutContacts();
			}else{
				clip_tips(dataObj['result']);
			}
		}
	});
}
function cancelEdit(obj){
	var _input = jQ(obj).parent().prevAll("li.editinput").find("input");
	_input.removeClass("edit").attr("disabled","disabled");
	_input.val(_input.attr("title"));
	var _parent=jQ(obj).parent();
	_parent.find("a.confirm").hide();
	_parent.find("a.cancel").hide();
	_parent.find("a.del").show();
	_parent.find("a.edit").show();
	
}
function createContactHtml(data,contentid,type){
	jQ("#"+contentid).empty();
	var html="";
	var currentpage=eval(jQ("currentpage",data).text());
	var pagecount=eval(jQ("pagecount",data).text());
	jQ("#"+contentid).data("currentpage",currentpage);
	jQ("#"+contentid).data("pagecount",pagecount);
	var contactlength=jQ(data).find("Contact").length;
	jQ(data).find("Contact").each(function(){
		var _id = jQ(this).find("id").text();
		var _uEmail = jQ(this).find("uEmail").text();
		var _uName = jQ(this).find("uName").text();
		var _name = jQ(this).find("name").text();
		var _email = jQ(this).find("email").text();
		var _mobile = jQ(this).find("mobile").text();
		var _uid=jQ(this).find("uid").text();
		var _birth=eval(jQ(this).find("birth").text());
		var _ulogo=jQ(this).find("ulogo").text();
		var _qq=jQ(this).find("qq").text();
		if(type!=0){
			_uName=_name;
			_uEmail=_email;
		}
		html+="<div class=\"element\" id=\"element-"+_id+"\">";
			html+="<div class=\"top\">";
				html+="<ul class=\"userinfo\">";
					html+="<li class=\"name\">"+_uName;
					if(type==0)
						html+="<a class='homepage' href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+_uid+"'></a>";
					html+="</li>";
					html+="<li class=\"editinput\"><span>备注名：</span><input disabled=\"disabled\" value="+_name+" title="+_name+"></li>";
					html+="<li><span>邮箱：</span><span class=\"val\">"+_uEmail+"</span></li>";
					if(type==0)
						html+="<li><span>手机：</span><span class=\"val\">"+_mobile+"</span></li>";
					html+="<li class=\"btn\">";
						if(type==0)
							html+="<a class=\"ext\" onclick=\"changeelementext(this,'element-"+_id+"');\"></a>";
						else
							html+="<a class=\"invite\" onclick=\"sendInvite(this,'element-"+_id+"');\"></a>";
						html+="<a class=\"del\" onclick=\"delContact(this,'element-"+_id+"')\"></a>";
						html+="<a class=\"edit\" onclick=\"editContact(this)\"></a>";
						html+="<a class=\"cancel\" onclick=\"cancelEdit(this)\"></a>";
						html+="<a class=\"confirm\" onclick=\"confirmEdit(this,'element-"+_id+"')\"></a>";
					html+="</li>";
				html+="</ul>";
				html+="<div class=\"userlogo\" style=\"background:url('"+_ulogo+"')\">";
				html+="</div>";
			html+="</div>";
			if(type==0){
				html+="<div class=\"bottom\">";
					html+="<div class=\"detailcontent\">";
						html+="<ul class=\"userinfo\">";
							html+="<li><span>生日：</span><span class=\"val\">"+formateDetailTime(_birth)+"</span></li>";
							html+="<li><span>QQ：</span><span class=\"val\">"+_qq+"</span></li>";
						html+="	</ul>";
					html+="</div>";
				html+="</div>";
			}
		html+="</div>";
	});
	html+=initPageing(currentpage,pagecount,contentid);
	jQ("#"+contentid).append(html);
	jQ("#"+contentid+" div.contactpaging a").unbind("click").bind("click",function(){
		var page=jQ(this).attr("page");
		var currentpage=0;
		if(page=="first"){
			currentpage=0;
		}else if(page=="last"){
			currentpage=pagecount-1;
		}else{
			currentpage=eval(page);
		}
		loadContactInfo(jQ(this).parent().parent().attr("id"),currentpage);
	});
}
function initPageing(currentpage,pagecount,contentid){
	var html="";
	if(pagecount>1){
		var pagemin=currentpage-2;
		var pagemax=currentpage+2;
		if(pagemin<0){
			pagemin=0;
		}
		if(pagemax>pagecount-1){
			pagemax=pagecount-1;
		}
		html+="<div class='contactpaging'>";
		html+="<a page='first'><span >&lt;&lt;</span></a>";
		for(var i=pagemin;i<=pagemax;i++){
			if(i==currentpage){
				html+="<b ><span>"+(i+1)+"</span></b>";
			}else
				html+="<a page='"+i+"'><span>"+(i+1)+"</span></a>";
		}
		html+="<a page='last'><span >&gt;&gt;</span></a>";
		html+="<span class='pagecount'>共有"+pagecount+"页</span>";
		html+="</div>";
		
	}
	return html;
}


function formateDetailTime(daytime){
	if(daytime==0)
		return "";
	else{
		var temp=new Date();
		temp.setTime(daytime);
		return temp.pattern("yyyy年MM月dd日");
	}
}
function loadContactInfo(contentid,currentpage){
	var type=eval(jQ("#"+contentid).data("type"));
	jQ.ajax({
		type:"POST",
		url: "contactAjax!contactInfo.action",
		data:{
			contactType : type,
			currentpage:currentpage
		},
		success:function(data){
			createContactHtml(data,contentid,type);
		}
	});
}

function getCountsAboutContacts(){
	
	jQ.ajax({
		type:"POST",
		url: "contactAjax!getCountsAboutContacts.action",
		success:function(data){
			var dataObj=eval("("+data+")");
			var strangercount=dataObj.strangercount;
			var contactcount=dataObj.contactcount;
			if(strangercount>99){
				strangercount="99+";
			}
			if(contactcount>99){
				contactcount="99+";
			}
			jQ("#strangercount").empty().append("("+strangercount+")");
			jQ("#contactcount").empty().append("("+contactcount+")");
			
		}
	});
}

function loadAllSubscribedUsers(){
	var currentpage=eval(jQ("#contactlistbtn").data("currentpage"));
	jQ.ajax({
		type:"POST",
		url: "contactAjax!getAllSubscribedUsers.action",
		data:{
			currentpage:currentpage
		},
		success:function(data){
			var allcount=eval(jQ(data).find("allcount").text());
			jQ("#contactcount").empty().append("("+allcount+")");
			
			jQ("#contactlistcontent").empty();
			var html="";
			var currentpage=eval(jQ("currentpage",data).text());
			var pagecount=eval(jQ("pagecount",data).text());
			jQ("#contactlistcontent").data("currentpage",currentpage);
			jQ("#contactlistcontent").data("pagecount",pagecount);
			var contactlength=jQ(data).find("Contact").length;
			jQ(data).find("Contact").each(function(){
				var _id = jQ(this).find("id").text();
				var _uName = jQ(this).find("uName").text();
				var _uEmail = jQ(this).find("uEmail").text();
				var _uid=jQ(this).find("uid").text();
				var _birth=eval(jQ(this).find("birth").text());
				var _ulogo=jQ(this).find("ulogo").text();
				var _qq=jQ(this).find("qq").text();
				html+="<div class=\"element\" id=\"element-"+_id+"\">";
					html+="<div class=\"top\">";
						html+="<ul class=\"userinfo\">";
							html+="<li class=\"name\">"+_uName;
								html+="<a class='homepage' href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+_uid+"'></a>";
							html+="</li>";
							html+="<li><span>邮箱：</span><span class=\"val\">"+_uEmail+"</span></li>";
							html+="<li class=\"btn\" style='margin-top: 48px;'>";
								html+="<a class=\"ext\" onclick=\"changeelementext(this,'element-"+_id+"');\"></a>";
							html+="</li>";
						html+="</ul>";
						html+="<div class=\"userlogo\" style=\"background:url('"+_ulogo+"')\">";
						html+="</div>";
					html+="</div>";
					html+="<div class=\"bottom\">";
						html+="<div class=\"detailcontent\">";
							html+="<ul class=\"userinfo\">";
								html+="<li><span>生日：</span><span class=\"val\">"+formateDetailTime(_birth)+"</span></li>";
								html+="<li><span>QQ：</span><span class=\"val\">"+_qq+"</span></li>";
							html+="	</ul>";
						html+="</div>";
					html+="</div>";
				html+="</div>";
			});
			html+=initPageing(currentpage,pagecount,"contactlistcontent");
			jQ("#contactlistcontent").append(html);
			jQ("#contactlistcontent div.contactpaging a").unbind("click").bind("click",function(){
				var page=jQ(this).attr("page");
				var currentpage=0;
				if(page=="first"){
					currentpage=0;
				}else if(page=="last"){
					currentpage=pagecount-1;
				}else{
					currentpage=eval(page);
				}
				jQ("#contactlistbtn").data("currentpage",currentpage);
				loadAllSubscribedUsers();
			});
		}
	});
}

function checkStatus(obj,event) {
	if(event.type=='focus'){
		if(jQ(obj).hasClass("default")){
			jQ(obj).removeClass("default").addClass("onit");
			jQ(obj).val("");
		}
	}else if(event.type=='blur'){
		if(jQ(obj).hasClass("onit")){
			if(jQ.trim(jQ(obj).val()).length==0){
				jQ(obj).removeClass("onit").addClass("default");
				if(eval(jQ("#search_type_sel").val())==1)
					jQ(obj).val("输入搜索的真实姓名");
				else
					jQ(obj).val("输入搜索的订阅商家名");
			}
		}
	}
}

function changeUserType(obj){
	var val=eval(jQ(obj).val());
	if(val==1){
		jQ("#searchinput").val("输入搜索的真实姓名");
		jQ("#searchinput").removeClass().addClass("add_ul_input").addClass("default");
	}else{
		jQ("#searchinput").val("输入搜索的订阅商家名");
		jQ("#searchinput").removeClass().addClass("add_ul_input").addClass("default");
	}
	
}

function searchUser(obj){
	var inputobj=jQ(obj).prev("input");
	var val=jQ.trim(inputobj.val());
	if(inputobj.hasClass("default"))
		val="";
	var tm= encodeURI(val);
	tm=encodeURI(tm);
	var searchtype=jQ("#search_type_sel").val();
	if(val.length==0){
		if(eval(searchtype)==1)
			jQ("#search_info").empty().append("输入您要搜索的档期用户。");
		else
			jQ("#search_info").empty().append("输入您要搜索的订阅商家名。");
		return;
	}else{
		var tm= encodeURI(val);
		tm=encodeURI(tm);
		window.location.href=jQ("#ctxVal").val()+"/contact/search.html?type="+searchtype+"&content="+tm;
	}
}

jQ(function(){
	var usertype=eval(jQ("#var_usertype").val());
	if(usertype==2){
		jQ("#contactlistbtn").hide();
		jQ("#pagetitle").empty().append("订阅用户");
		//jQ("#contactlistbtn li").hide();
		//jQ("#contactlistbtn li:first").show();
		//jQ("#contactlistbtn li:first span:first").empty().append("订阅用户");
		jQ("#contactlistbtn").data("currentpage",0);
		loadAllSubscribedUsers();
	}else{
		jQ("#contactlistbtn li").unbind("click").bind("click",function(){
			if(jQ(this).hasClass("onit")){
				return;
			}else{
				var zoneid=jQ("#contactlistbtn li.onit").attr("zone");
				jQ("#"+zoneid).hide();
				jQ("#contactlistbtn li.onit").removeClass("onit");
				jQ(this).addClass("onit");
				var showzoneid=jQ(this).attr("zone");
				jQ("#"+showzoneid).show();
				loadContactInfo(showzoneid,0);
			}
			
		});
		jQ("#contactlistbtn").show();
		jQ("#pagetitle").empty().append("好友名片");
		jQ("#contactlistcontent").data("type",0);
		jQ("#strangerlistcontent").data("type",4);
		getCountsAboutContacts();
		loadContactInfo("contactlistcontent",0);
	}
});